package com.oneonefive.educationsystem.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oneonefive.educationsystem.domain.LabRepair;
import com.oneonefive.educationsystem.domain.User;
import com.oneonefive.educationsystem.dto.Result;
import com.oneonefive.educationsystem.mapper.LabRepairMapper;
import com.oneonefive.educationsystem.service.LabRepairService;
import com.oneonefive.educationsystem.utils.StateChangeUtil;
import com.oneonefive.educationsystem.vo.LabRepairVo;
import io.netty.util.internal.StringUtil;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class LabRepairServiceImpl extends ServiceImpl<LabRepairMapper, LabRepair>
    implements LabRepairService{

    @Resource
    LabRepairMapper labRepairMapper;

    @Override
    public Result labRepList(LabRepairVo labRepairVo, User user) {
        int page = (labRepairVo.getPage() - 1) * 10;
        labRepairVo.setPage(page);
        long total = labRepairMapper.adminLabRepListCount(labRepairVo, user.getUserId()).size();
        List<LabRepair> list = labRepairMapper.adminLabRepList(labRepairVo, user.getUserId());
        List<LabRepairVo> resList = getResVo(list);
        return Result.ok(resList,total);
    }

    public List<LabRepairVo> getResVo(List<LabRepair> list) {
        List<LabRepairVo> resList = new ArrayList<>();
        for (LabRepair labRepair : list) {
            LabRepairVo labRepairVo = new LabRepairVo();
            BeanUtils.copyProperties(labRepair, labRepairVo);
            labRepairVo.setStateString(StateChangeUtil.labRepChange(labRepair.getState()));
            resList.add(labRepairVo);
        }
        return resList;
    }

    @Override
    public Result legitimacy(LabRepair labRepair) {
        if (labRepair.getLabId().length() > 10) {
            return Result.fail("实验室编号过长");
        }
        if (labRepair.getDes().length() > 200) {
            return Result.fail("描述过长");
        }
        return Result.ok();
    }


    @Override
    public Result stateChangeTo1(LabRepair labRepair) {
        QueryWrapper<LabRepair> wrapper = new QueryWrapper<>();
        wrapper.eq("id", labRepair.getId());
        List<LabRepair> list = labRepairMapper.selectList(wrapper);
        if (list.size() == 0 | list.size() > 1) {
            return Result.fail("找不到唯一的保修申请");
        }
        if(!StringUtil.isNullOrEmpty(labRepair.getExpl())){
            list.get(0).setExpl(labRepair.getExpl());
        }
        list.get(0).setState(labRepair.getState());
        boolean b = updateById(list.get(0));
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.fail("修改失败");
    }

//    @SneakyThrows
    @Override
    public Result labRepForTeaList(LabRepairVo labRepairVo, User user) {

        int page = (labRepairVo.getPage() - 1) * 10;
        labRepairVo.setPage(page);
        long total = labRepairMapper.teacherLabRepListCount(labRepairVo, user.getUserId()).size();
        List<LabRepair> list = labRepairMapper.teacherLabRepList(labRepairVo, user.getUserId());
        List<LabRepairVo> resList = getResVo(list);
        return Result.ok(resList, total);
    }

}




